package com.panopset.gui;

import static com.panopset.Util.x;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.panopset.swing.Document;
import com.panopset.swing.FontManager;

/**
 * Abstract table editor.
 *
 * @author Karl Dinwiddie
 *
 */
public abstract class AbstractTableEditor extends Document {

    /**
     * @param defaultResourcePath Default resource path.
     * @param fileName
     *            Document file name.
     */
    public AbstractTableEditor(final String defaultResourcePath,
            final String fileName) {
        super(defaultResourcePath, fileName);
    }

    /**
     * Table.
     */
    private JTable table;

    /**
     * @return Table model.
     */
    protected abstract DefaultTableModel createModel();

    /**
     * Model.
     */
    private DefaultTableModel model;

    /**
     * @return Table model.
     */
    protected final DefaultTableModel getModel() {
        if (model == null) {
            model = createModel();
        }
        return model;
    }

    /**
     * @return Table.
     */
    public final JTable getTable() {
        if (table == null) {
            table = new JTable(getModel());
            FontManager.register(table, FontManager.FontType.monospace);
            table.setAutoCreateRowSorter(true);
            table.getColumnModel().getColumn(0).sizeWidthToFit();
        }
        return table;
    }

    /**
     * Delete selected row.
     */
    final void doDelete() {
        int i = getTable().getSelectedRow();
        if (i > -1 && i < model.getRowCount()) {
            getModel().removeRow(table.convertRowIndexToModel(i));
        }
    }

    /**
     * Add a row.
     */
    protected abstract void doAdd();

    /**
     * Panel.
     */
    private JPanel panel;

    /**
     * @return Panel.
     */
    public final JPanel getPanel() {
        if (panel == null) {
            panel = new TPanel(new BorderLayout());
            panel.add(BorderLayout.CENTER, new JScrollPane(getTable()));
            TPanel cp = new TPanel();
            cp.add(new TButton("Add", 'a', x("Add a blank row") + ".",
                    new ActionListener() {
                        @Override
                        public void actionPerformed(final ActionEvent e) {
                            AbstractTableEditor.this.doAdd();
                        }

                    }));
            cp.add(new TButton("Delete", 'd', "Delete selected row" + ".",
                    new ActionListener() {
                        @Override
                        public void actionPerformed(final ActionEvent e) {
                            AbstractTableEditor.this.doDelete();
                        }

                    }));
            panel.add(BorderLayout.SOUTH, cp);
        }
        return panel;
    }

    /**
     * Value column header.
     */
    private String valueColumnHeader = "Value";

    /**
     * @return Value column header.
     */
    public final String getValueColumnHeader() {
        return valueColumnHeader;
    }

    /**
     * @param value
     *            New value column header.
     */
    public final void setColumnHeader(final String value) {
        valueColumnHeader = value;
    }

}
